package com.team7.home.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team7.common.entity.Course;
import com.team7.common.entity.Zone;
import com.team7.common.vo.CommonResult;
import com.team7.common.vo.ZoneVo;
import com.team7.home.dao.ZoneCourseDao;
import com.team7.home.dao.ZoneDao;
import com.team7.home.feign.CoreFeign;
import com.team7.home.service.ZoneService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @program: team7-object-master
 * @description:
 * @author: 王金涛
 * @create: 2021-10-19 14:51
 **/
@Service
public class ZoneServiceImpl extends ServiceImpl<ZoneDao, Zone> implements ZoneService {

    @Resource
    private ZoneDao zoneDao;

    @Resource
    private ZoneCourseDao zoneCourseDao;

    @Resource
    private CoreFeign coreFeign;

    /**
     * 查询分页方法
     * @param page
     * @param limit
     * @param zoneVo
     * @return
     */
    @Override
    public CommonResult getAllZone(Integer page, Integer limit, ZoneVo zoneVo) {
        Page<Zone> zonePage = new Page<>(page, limit);
        QueryWrapper<Zone> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        if (StringUtils.isNotEmpty(zoneVo.getZoneName())){
            wrapper.like("zone_name",zoneVo.getZoneName());
        }
        if (zoneVo.getIsDisable()!=null){
            wrapper.eq("is_disable",zoneVo.getIsDisable());
        }
        Page<Zone> zonePage1 = zoneDao.selectPage(zonePage, wrapper);
        if (zonePage1.getRecords().size()>0){
            return new CommonResult(2000,"查询成功",zonePage1);
        }else {
            return new CommonResult(5000,"查询失败",null);
        }
    }

    /**
     * 修改选中状态
     * @param id
     * @param isDisable
     * @return
     */
    @Override
    public CommonResult changeStatus(String id, Integer isDisable) {
        Zone zone = new Zone();
        zone.setId(id);
        zone.setIsDisable(isDisable);
        int i = zoneDao.updateById(zone);
        if (i>0){
            return new CommonResult(2000,"更改成功",null);
        }else {
            return new CommonResult(5000,"更改失败",null);
        }
    }

    @Override
    public CommonResult addZone(ZoneVo zoneVo) {
        QueryWrapper<Zone> wrapper = new QueryWrapper<>();
        Zone zone = new Zone();
        //进行判断 空表示新增 否则更新
        if (StringUtils.isNotEmpty(zoneVo.getId())){
            zone.setId(zoneVo.getId());
            zone.setGmtModified(LocalDateTime.now());
            zone.setZoneName(zoneVo.getZoneName());
            zone.setZoneDesc(zoneVo.getZoneDesc());

            int i = zoneDao.updateById(zone);
            if (i>0){
                return new CommonResult(2000,"更新成功",null);
            }else {
                return new CommonResult(5000,"更新失败",null);
            }

        }else {

            String zoneName = zoneVo.getZoneName();
            System.out.println(zoneName);
            wrapper.eq("zone_name",zoneName);
            wrapper.eq("is_deleted",0);
            Zone zone2 = zoneDao.selectOne(wrapper);

            if (zone2!=null&&zone2.getZoneName().equals(zoneName)) {
                return new CommonResult(5000,"添加失败,已经有了",null);
            }else {
                zone.setGmtCreate(LocalDateTime.now());
                zone.setGmtModified(LocalDateTime.now());
                zone.setZoneName(zoneVo.getZoneName());
                zone.setZoneDesc(zoneVo.getZoneDesc());
                zone.setIsDisable(0);
                zone.setIsDeleted(0);
                int i = zoneDao.insert(zone);
                if (i>0){
                    return new CommonResult(2000,"新增成功",null);
                }else {
                    return new CommonResult(5000,"新增失败",null);
                }
            }
        }
    }

    @Override
    public CommonResult deleteZone(String id) {
        Zone zone = new Zone();
        zone.setId(id);
        zone.setIsDeleted(1);
        int i = zoneDao.deleteById(zone);
        if (i>0){
            return new CommonResult(2000,"删除成功",null);
        }else {
            return new CommonResult(5000,"删除失败",null);
        }
    }

    /**
     * 获取所有课程
     * @return
     */
    @Override
    public CommonResult getAllQianZone() {
        QueryWrapper<Zone> wrapper = new QueryWrapper<>();
        wrapper.eq("is_disable",0);
        wrapper.eq("is_deleted",0);
        List<Zone> zones = zoneDao.selectList(wrapper);
        for (Zone zone:zones) {
            List<Course> allCourseById = zoneCourseDao.getAllCourseById(zone.getId());
            zone.setCourseList(allCourseById);
        }
        return new CommonResult(2000,"成功",zones);
    }

    @Override
    public CommonResult getAllQianMenu() {
        CommonResult allMenu = coreFeign.getAllMenu();
        return allMenu;
    }
}
